package com.example.personal.aiagent.chat.controller;

import com.example.personal.aiagent.chat.commonservice.ChatException;
import com.example.personal.aiagent.chat.commonservice.ChatPool;
import com.example.personal.aiagent.chat.commonservice.ChatService;
import com.example.personal.aiagent.chat.localservice.LocalChatAbility;
import com.example.personal.aiagent.chat.model.vo.ChatRequest;
import com.example.personal.aiagent.common.constant.ErrorCode;
import com.example.personal.aiagent.common.model.vo.CommonEmptyResponse;
import com.example.personal.aiagent.common.model.vo.CommonResponse;
import com.example.personal.aiagent.common.util.UserUtil;
import com.example.personal.aiagent.xunfei.account.model.dto.AccountDTO;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.Size;
import java.util.Collections;
import java.util.Optional;

/**
 * ChatController
 *
 * @author blacksea3(jxt) 2023/12/3 20:05
 */
@Controller
@RequestMapping("/chat")
@Slf4j
public class ChatController {
    //    @Autowired
    //    private XunFeiAbility xunfeiAbility;

    @Autowired
    private ChatPool chatPool;

    @ResponseBody
    @PostMapping("/start")
    public CommonEmptyResponse start() {
        String userName = UserUtil.getCurrentUser();
        chatPool.insertUserChat(userName, new LocalChatAbility(new AccountDTO()));
        Optional<ChatService> localChatAbility = chatPool.getChatService(userName);
        if (localChatAbility.isEmpty()) {
            throw new ChatException(ErrorCode.CHAT_ENTITY_NOT_FOUND, Collections.emptyList());
        }
        localChatAbility.get().startChat();
        return new CommonEmptyResponse();
    }

    @ResponseBody
    @PostMapping("/input")
    public CommonEmptyResponse input(
        @RequestBody @Validated ChatRequest chatRequest) {
        String userName = UserUtil.getCurrentUser();
        Optional<ChatService> localChatAbility = chatPool.getChatService(userName);
        if (localChatAbility.isEmpty()) {
            throw new ChatException(ErrorCode.CHAT_ENTITY_NOT_FOUND, Collections.emptyList());
        }
        localChatAbility.get().inputChatContent(chatRequest.getContent());
        return new CommonEmptyResponse();
    }

    @ResponseBody
    @GetMapping("/get_result")
    public CommonResponse<String> getResult() {
        String userName = UserUtil.getCurrentUser();
        Optional<ChatService> localChatAbility = chatPool.getChatService(userName);
        if (localChatAbility.isEmpty()) {
            throw new ChatException(ErrorCode.CHAT_ENTITY_NOT_FOUND, Collections.emptyList());
        }
        return new CommonResponse<>(localChatAbility.get().getChatResult());
    }

    @ResponseBody
    @PostMapping("/close")
    public CommonEmptyResponse close() {
        String userName = UserUtil.getCurrentUser();
        Optional<ChatService> localChatAbility = chatPool.getChatService(userName);
        if (localChatAbility.isPresent()) {
            localChatAbility.get().closeChat();
            chatPool.deleteUserChat(userName);
        } else {
            throw new ChatException(ErrorCode.CHAT_ENTITY_NOT_FOUND, Collections.emptyList());
        }
        return new CommonEmptyResponse();
    }

    @ResponseBody
    @PostMapping("/force_close")
    public CommonEmptyResponse forceClose() {
        String userName = UserUtil.getCurrentUser();
        Optional<ChatService> localChatAbility = chatPool.getChatService(userName);
        localChatAbility.ifPresent(ChatService::forceCloseChat);
        chatPool.deleteUserChat(userName);
        return new CommonEmptyResponse();
    }
}
